using System;
using System.Linq;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using ReaderOTR2000Demo;
using ReaderOTR2000Demo_WinCE;
using System.Threading;

namespace ReaderOTR2000Demo_CE
{
    public partial class FrmOTR2000CE : Form
    {
        public int m_hScanner = -1, m_hSocket = -1, OK = 0;
        public int nBaudRate = 0, Interval, EPC_Word;
        public byte connect_OK = 0;

        ReaderOTR2000API.ReaderOTR2000Param Param = new ReaderOTR2000API.ReaderOTR2000Param();

        public int k;

        public int nidEvent, mem, ptr, len;
        public int Read_times;
        public int m_antenna_sel;
        public byte Mask;
        public byte[] AccessPassword = new byte[4];
        public byte[] mask = new byte[96];
        public byte[] IDTemp = new byte[12];
        public byte[,] TagBuffer = new byte[100, 32];
        public byte[] AccessPassWord = new byte[4];
        public byte[,] TagNumber = new byte[100, 255];

        public int count_test = 0;

        // Hardware & Software Version
        public int HardVersion, SoftVersion;

        public FrmOTR2000CE()
        {
            InitializeComponent();
        }

        private void FrmOTR2000CE_Load(object sender, EventArgs e)
        {
            listView1.FullRowSelect = true;
            //ListViewItem item = new ListViewItem();
            //item.SubItems[0].Text = "1";
            //item.SubItems.Add("ab");
            //item.SubItems.Add("ac");
            //item.SubItems.Add("ad");
            //item.SubItems.Add("ae");
            //listView1.Items.Add(item);
            //string strtemp;
            //strtemp = listView1.Items[0].SubItems[0].Text;
            //strtemp = listView1.Items[0].SubItems[1].Text;
            //strtemp = listView1.Items[0].SubItems[2].Text;
            //strtemp = listView1.Items[0].SubItems[3].Text;
            //strtemp = listView1.Items[0].SubItems[4].Text;

            //Loading
            #if WINCED
            Device.BthPowerOff();
            Device.BthPowerOn(); 
            #endif

            int res;
            string szPort;
            
            #if WINCED
            szPort = "COM3:";
            #else
            szPort = "COM1";
            #endif
            
            res = ReaderOTR2000API.ConnectScanner(ref m_hScanner, szPort, 0);
            if (res == 0)
            {

                res = ReaderOTR2000API.GetReaderVersion(m_hScanner, ref HardVersion, ref SoftVersion);
                if ( res == 0 )
                {
                    ReaderParam();
                }
                else
                {
                    MessageBox.Show("GetReaderVersion error!");
                }
                //MessageBox.Show("Connect success!");
            }
            else
            {
                MessageBox.Show("Connect false!");
            }

        }

        private void FrmOTR2000CE_Closed(object sender, EventArgs e)
        {
            //Closed
            ReaderOTR2000API.DisconnectScanner(m_hScanner);
            #if WINCED
            Device.BthPowerOff();
            #endif

        }

        private void button24_Click_1(object sender, EventArgs e)
        {
            //Update Parameter
            //Update parameters
            if (m_hScanner == -1)
                return;
            int WorkMode = 0, TagType = 0;
            string[] ipinfo = new string[4];
            byte[] readerip = new byte[4];
            byte[] mask = new byte[4];
            byte[] gateway = new byte[4];
            byte[] Password = new byte[2];
            

            if (radioButton29.Checked == true)
                WorkMode = 0;
            if (radioButton30.Checked == true)
                WorkMode = 1;

            TagType = 4;//6C

            
            Param.BaudRate = (byte)(comboBox13.SelectedIndex + 4);
            Param.Power = Convert.ToByte(textBox27.Text);
            Param.Min_Frequence = (byte)(comboBox14.SelectedIndex + 1);
            Param.Max_Frequence = (byte)(comboBox15.SelectedIndex + 1);
            Param.WorkMode = (byte)WorkMode;
            Param.NumofCard = Convert.ToByte(textBox29.Text);
            Param.TagType = (byte)TagType;
            



            int res = ReaderOTR2000API.WriteParam(m_hScanner, ref Param);
            
            if (res != 0)
            {
                MessageBox.Show("Fail to update rReader parameter!");
                return;
            }

            
            int[] br = new int[] { 9600, 19200, 38400, 57600, 115200 };
            int baud = br[Param.BaudRate - 4];

            ReaderOTR2000API.DCB dcb = new ReaderOTR2000API.DCB();
            ReaderOTR2000API.GetCommState(m_hScanner, ref dcb);
            dcb.BaudRate = baud;
            ReaderOTR2000API.SetCommState(m_hScanner, ref dcb);

            ReaderOTR2000API.PurgeComm(m_hScanner, 0x04);
            ReaderOTR2000API.PurgeComm(m_hScanner, 0x08);

            
            res = ReaderOTR2000API.Reboot(m_hScanner);
            

            if (res == 0)
            {
                MessageBox.Show("Update Reader Parameter successfully!");
            }
            else
            {
                MessageBox.Show("Fail to reboot reader!", "Warning");
            }


        }

        public int ReaderParam()
        {
            int i = 0, NumofFreq = 0;
            string strtemp = "";
            double Freq = 0, jumpFreq = 0, temp = 0;
            bool ischina = false, AM = false;

            byte[] readerIP = new byte[4];
            byte[] mask = new byte[4];
            byte[] gateway = new byte[4];
            
            
            //ReaderOTR2000API.Reader1200AutoParam AutoParam = new ReaderOTR2000API.Reader1200AutoParam();
            int res =   0;
            for (i = 0; i < 3; i++)
            {
                res = ReaderOTR2000API.ReadParam(m_hScanner, ref Param);
                if (res == 0) break;
            }
            
            

            if (res != 0)
            {
                MessageBox.Show("Fail to read parameter of reader!");
                return res;
            }

            int softver = 0;

            switch ((HardVersion >> 8) & 0xFF)
            {
                case 0x17:
                    softver = (SoftVersion >> 8) & 0xFF;
                    break;
                case 0x1A:
                    softver = SoftVersion & 0xFF;
                    break;
            }

            switch (softver)
            {
                case 0x00:
                    NumofFreq = 63;
                    Freq = 902.6;
                    jumpFreq = 0.4;
                    break;
                case 0x01:
                    NumofFreq = 125;
                    Freq = 902.6;
                    jumpFreq = 0.2;
                    break;
                case 0x02:
                    NumofFreq = 11;
                    Freq = 865.5;
                    jumpFreq = 0.2;
                    break;
                case 0x03:
                    NumofFreq = 8;
                    Freq = 902.6;
                    jumpFreq = 3.6;
                    break;
                case 0x04:
                    NumofFreq = 10;
                    Freq = 952.2;
                    jumpFreq = 0.2;
                    break;
                case 0x05:
                    NumofFreq = 42;
                    Freq = 919.2;
                    jumpFreq = 0.2;
                    break;
                case 0x06:
                    NumofFreq = 16;
                    Freq = 920.625;
                    jumpFreq = 0.25;
                    ischina = true;
                    break;
                //Korea
                case 0x07:
                    NumofFreq = 16;
                    Freq = 910.5;
                    jumpFreq = 0.2;
                    break;
                case 0x08:
                    NumofFreq = 50;
                    Freq = 902.75;
                    jumpFreq = 0.5;
                    AM = true;
                    break;
                case 0x09:
                    NumofFreq = 16;
                    Freq = 840.625;
                    jumpFreq = 0.25;
                    ischina = true;
                    break;
                default:
                    NumofFreq = 63;
                    Freq = 902.6;
                    jumpFreq = 0.4;
                    break;
            }

            for (i = 0; i < NumofFreq; i++)
            {
                if (ischina)
                {
                    temp = Freq + i * jumpFreq;
                    strtemp = string.Format("{0,0:D}{1,0:s}{2,7:F03}", i + 1, ":", temp);
                }
                else
                {
                    if (AM == true)
                    {
                        temp = Freq + i * jumpFreq;
                        strtemp = string.Format("{0,0:D}{1,0:s}{2,6:F02}", i + 1, ":", temp);

                    }
                    else
                    {
                        temp = Freq + i * jumpFreq;
                        strtemp = String.Format("{0,0:D}{1,0:s}{2,4:F01}", i + 1, ":", temp);
                    }

                }
                comboBox14.Items.Add(strtemp);
                comboBox15.Items.Add(strtemp);
            }

            if ((Param.BaudRate - 4) < 0)
                comboBox13.SelectedIndex = 3;
            else
                comboBox13.SelectedIndex = Param.BaudRate - 4;
            comboBox14.SelectedIndex = Param.Min_Frequence - 1;
            comboBox15.SelectedIndex = NumofFreq - 1;//Param.Max_Frequence - 1;
            textBox27.Text = Param.Power.ToString();
            textBox29.Text = Param.NumofCard.ToString();

            switch (Param.WorkMode)
            {
                case 0:
                    radioButton29.Checked = true;
                    break;
                case 1:
                    radioButton30.Checked = true;
                    break;
            }

            

            if (res != 0)
            {
                MessageBox.Show("Fail to read Autoparameter of reader!");
                return res;
            }

            
            return 0;
        }

        private void button24_Click(object sender, EventArgs e)
        {

        }

        private void button3_Click(object sender, EventArgs e)
        {

        }

        private void button6_Click(object sender, EventArgs e)
        {

        }

        private void button5_Click(object sender, EventArgs e)
        {

        }

        #region List Tag ID
        private void button3_Click_1(object sender, EventArgs e)
        {
            //List tag
            //List tag
            if (m_hScanner == -1)
                return;
            nidEvent = 1;
            string str, strtemp;

            //Address range 0-95
            if (Convert.ToInt32(textBox1.Text) > 95 || Convert.ToInt32(textBox1.Text) < 0)
            {
                MessageBox.Show("Please input start address of data between 0 and 95!");
                textBox1.Focus();
                textBox1.SelectAll();
                button3.Text = "List Tag ID";
                return;
            }

            //Length range 0-96
            if (Convert.ToInt32(textBox2.Text) > 96 || Convert.ToInt32(textBox2.Text) < 0)
            {
                MessageBox.Show("Please input length of data between 0 and 96 !");
                textBox2.Focus();
                textBox2.SelectAll();
                button3.Text = "List Tag ID";
                return;
            }

            //The actual data
            str = textBox3.Text;
            int str_len = textBox3.Text.Length;
            int i, m, j;

            if (Convert.ToInt32(textBox2.Text) == 0)
                m = 0;
            else
            {
                m = Convert.ToInt32(textBox2.Text) / 8;
                for (i = 0; i < m; i++)
                {
                    str += "00";
                }
                if (Convert.ToInt32(textBox2.Text) % 8 != 0)
                {
                    for (i = 0; i < ((m + 1) * 2 - str_len); i++)
                        str += "0";
                    m++;
                }
            }

            if (radioButton1.Checked == true)
                mem = 0;
            if (radioButton2.Checked == true)
                mem = 1;
            if (radioButton3.Checked == true)
                mem = 2;
            if (radioButton4.Checked == true)
                mem = 3;

            ptr = Convert.ToInt16(textBox1.Text);
            len = Convert.ToInt16(textBox2.Text);

            for (i = 0; i < m; i++)
            {
                strtemp = str[i * 2].ToString() + str[i * 2 + 1].ToString();
                mask[i] = Convert.ToByte(strtemp);
            }
            int[] timrinterval = new int[] { 10, 20, 30, 50, 100, 200, 500 };
            
            if (button3.Text == "List Tag ID")
            {
                
                button3.Text = "Stop";
                Read_times = 0;
                k = 0;
                count_test = 0;
                listView1.Items.Clear();
                timer1.Interval = 50;
                timer1.Enabled = true;
            }
            else
            {
                button3.Text = "List Tag ID";
                timer1.Enabled = false;
                comboBox3.Items.Clear();
                
                byte[] dada = new byte[1024];
                for (i = 0; i < k; i++)
                {
                    str = listView1.Items[i].SubItems[2].Text;
                    TagBuffer[i, 0] = Convert.ToByte(str);
                    str = listView1.Items[i].SubItems[1].Text;
                    for (j = 0; j < str.Length / 2; j++)
                    {
                        strtemp = str[j * 2].ToString() + str[j * 2 + 1].ToString();
                        TagBuffer[i, j + 1] = (byte)Convert.ToInt16(strtemp, 16);
                    }
                    strtemp = "";
                    strtemp = (i + 1).ToString("D2") + ".";
                    comboBox3.Items.Add(strtemp + str);
                    
                }
                if (k != 0)
                {
                    comboBox3.SelectedIndex = 0;
                    
                }
            }

        }
        #endregion


        #region Timer
        private void timer1_Tick(object sender, EventArgs e)
        {
            
        }

        private void timer1_Tick_1(object sender, EventArgs e)
        {
            int res;
            int i, j, nCounter = 0, ID_len = 0, ID_len_temp = 0, be_antenna, success;
            string str, str_temp, strtemp;
            byte[] DB = new byte[128];
            byte[] IDBuffer = new byte[7680];

            Read_times++;
            //Thread.Sleep(Interval);

            be_antenna = 1;


            if (be_antenna != 0)
            {

                ListViewItem item = new ListViewItem();
                switch (nidEvent)
                {
                    case 1:
                        Array.Clear(TagBuffer, 0, TagBuffer.Length);
                        count_test++;

                        res = ReaderOTR2000API.EPC1G2_ReadLabelID(m_hScanner, mem, ptr, len, mask, IDBuffer, ref nCounter);

                        if (res == OK)
                        {
                            for (i = 0; i < nCounter; i++)
                            {
                                if (IDBuffer[ID_len] > 15)
                                {
                                    nCounter = 0;
                                    break;
                                }
                                ID_len_temp = IDBuffer[ID_len] * 2 + 1;
                                for (j = 0; j < ID_len_temp; j++)
                                {
                                    TagBuffer[i, j] = IDBuffer[ID_len + j];
                                }
                                ID_len += ID_len_temp;
                            }
                            for (i = 0; i < nCounter; i++)
                            {
                                str = "";
                                strtemp = "";
                                ID_len = TagBuffer[i, 0] * 2;
                                for (j = 0; j < ID_len; j++)
                                {
                                    strtemp = TagBuffer[i, j + 1].ToString("X2");
                                    str += strtemp;
                                }

                                for (j = 0; j < k; j++)
                                {
                                    strtemp = listView1.Items[j].SubItems[2].Text;
                                    if (ID_len == Convert.ToInt32(strtemp) * 2)
                                    {
                                        str_temp = listView1.Items[j].SubItems[1].Text;
                                        if (str == str_temp)
                                        {
                                            success = Convert.ToInt32(listView1.Items[j].SubItems[3].Text) + 1;
                                            listView1.Items[j].SubItems[3].Text = success.ToString();
                                            break;
                                        }
                                    }
                                }
                                if (j == k)
                                {
                                    //item = listView1.Items.Add((k + 1).ToString());
                                    item.SubItems[0].Text = (k + 1).ToString();
                                    item.SubItems.Add(str);
                                    item.SubItems.Add(TagBuffer[i, 0].ToString("X2"));
                                    success = 1;
                                    item.SubItems.Add(success.ToString());
                                    item.SubItems.Add(count_test.ToString());
                                    k++;
                                    listView1.Items.Add(item);
                                }
                            }

                        }
                        for (j = 0; j < k; j++)
                        {
                            listView1.Items[j].SubItems[4].Text = count_test.ToString();
                        }

                        break;


                    case 2:

                        res = ReaderOTR2000API.EPC1G2_ReadWordBlock(m_hScanner, Convert.ToByte(EPC_Word), IDTemp, Convert.ToByte(mem), Convert.ToByte(textBox5.Text), Convert.ToByte(textBox6.Text), DB, AccessPassWord);
                        
                        if (res == OK)
                        {
                            str = "";
                            for (i = 0; i < Convert.ToByte(textBox6.Text) * 2; i++)
                            {
                                strtemp = DB[i].ToString("X2");
                                str += strtemp;
                            }
                            listBox1.Items.Add(str);

                        }
                        break;


                    case 3:

                        res = ReaderOTR2000API.ISO6B_ReadLabelID(m_hScanner, TagNumber, ref nCounter);

                        if (res == OK)
                        {
                            
                            for (i = 0; i < nCounter; i++)
                            {
                                str = "";
                                for (j = 0; j < 8; j++)
                                {
                                    str += TagNumber[i, j].ToString("X2");
                                }

                                for (j = 0; j < k; j++)
                                {
                                    str_temp = listView2.Items[j].SubItems[1].Text;
                                    if (str == str_temp)
                                    {
                                        success = Convert.ToInt32(listView2.Items[j].SubItems[2].Text) + 1;
                                        listView2.Items[j].SubItems[2].Text = success.ToString();
                                        break;
                                    }
                                }
                                if (j == k)
                                {
                                    //item = listView2.Items.Add((k + 1).ToString(), k);
                                    item.SubItems[0].Text = (k + 1).ToString();
                                    item.SubItems.Add(str);
                                    success = 1;
                                    item.SubItems.Add(success.ToString());
                                    //item.SubItems.Add(count_test.ToString());
                                    success = str.Length;
                                    item.SubItems.Add("12");
                                    item.SubItems.Add("50");
                                    k++;
                                    listView2.Items.Add(item);
                                }

                            }

                        }
                        for (j = 0; j < k; j++)
                        {
                            //listView2.Items[j].SubItems[3].Text = count_test.ToString();
                        }



                        //                   Thread.Sleep(100);

                        break;

                    case 4:

                        res = ReaderOTR2000API.ISO6B_ReadByteBlock(m_hScanner, IDTemp, Convert.ToByte(ptr), Convert.ToByte(len), mask);

                        if (res == OK)
                        {
                            str = "HEX:";
                            for (i = 0; i < len; i++)
                            {
                                str_temp = mask[i].ToString("X2");
                                str += str_temp;
                            }
                            listBox2.Items.Add(str);
                            listBox2.Items.Add("---------------------------------");
                        }

                        break;

                    case 5:

                        res = ReaderOTR2000API.ISO6B_WriteByteBlock(m_hScanner, IDTemp, Convert.ToByte(ptr), Convert.ToByte(len), mask);

                        if (res == OK)
                        {
                            listBox2.Items.Add("Write Successfully!");
                            listBox2.Items.Add("---------------------------------");
                        }
                        else
                        {
                            listBox2.Items.Add("Write Fail!");
                            listBox2.Items.Add("---------------------------------");
                        }
                        break;




                }
            }


        }
        #endregion


        #region Read
        private void button5_Click_1(object sender, EventArgs e)
        {
            if (m_hScanner == -1)
                return;
            int i;
            string str;
            nidEvent = 2;
            if (comboBox3.SelectedIndex < 0)
            {
                MessageBox.Show("Please identify a tag first!");
                return;
            }
            if (Convert.ToInt16(textBox5.Text) < 0)
            {
                MessageBox.Show("Please input start address of tag more then 0!");
                textBox5.Focus();
                textBox5.SelectAll();
                return;
            }
            if (Convert.ToInt16(textBox6.Text) < 1)
            {
                MessageBox.Show("Please input length of data more than 1!");
                textBox6.Focus();
                textBox6.SelectAll();
                return;
            }
            if (textBox7.Text.Length != 8)
            {
                MessageBox.Show("Please input correct accesspassword!");
                textBox7.Focus();
                textBox7.SelectAll();
                return;
            }
            str = textBox7.Text;
            for (i = 0; i < 4; i++)
            {
                AccessPassWord[i] = Convert.ToByte(str[i * 2] + str[i * 2 + 1]);
            }

            EPC_Word = TagBuffer[comboBox3.SelectedIndex, 0];
            for (i = 0; i < TagBuffer[comboBox3.SelectedIndex, 0] * 2; i++)
            {
                IDTemp[i] = TagBuffer[comboBox3.SelectedIndex, i + 1];
            }
            if (radioButton5.Checked == true)
                mem = 0;
            if (radioButton6.Checked == true)
                mem = 1;
            if (radioButton7.Checked == true)
                mem = 2;
            if (radioButton8.Checked == true)
                mem = 3;


            int[] timrinterval = new int[] { 10, 20, 30, 50, 100, 200, 500 };
            Interval = 50;
            if (button5.Text == "Read")
            {
                m_antenna_sel = 1;
                
                button5.Text = "Stop";
                Read_times = 0;
                k = 0;
                listBox1.Items.Clear();
                timer1.Interval = Interval;
                timer1.Enabled = true;
            }
            else
            {
                button5.Text = "Read";
                timer1.Enabled = false;
            }

        }
        #endregion


        #region Write
        private void button6_Click_1(object sender, EventArgs e)
        {
            int res =   -1;
            if (m_hScanner == -1)
                return;
            int i;
            string str;
            listBox1.Items.Clear();

            if (textBox7.Text.Length != 8)
            {
                MessageBox.Show("Please input correct accesspassword!");
                textBox7.Focus();
                textBox7.SelectAll();
                return;
            }

            str = textBox7.Text;
            for (i = 0; i < 4; i++)
            {
                AccessPassWord[i] = (byte)Convert.ToInt16((str[i * 2].ToString() + str[i * 2 + 1].ToString()), 16);
            }

            if (radioButton5.Checked == true)
                mem = 0;
            if (radioButton6.Checked == true)
                mem = 1;
            if (radioButton7.Checked == true)
                mem = 2;
            if (radioButton8.Checked == true)
                mem = 3;
            switch (mem)
            {
                case 0:
                case 2:
                    if (Convert.ToInt16(textBox6.Text) < 1 || Convert.ToInt16(textBox6.Text) > 4)
                    {
                        MessageBox.Show("Please input Length of Tag Data between 1 and 4 Word !");
                        textBox6.Focus();
                        textBox6.SelectAll();
                        return;
                    }
                    break;
                case 1:
                    if (Convert.ToInt16(textBox6.Text) < 1 || Convert.ToInt16(textBox6.Text) > 6)
                    {
                        MessageBox.Show("Please input Length of Tag Data between 1 and 6 Word !");
                        textBox6.Focus();
                        textBox6.SelectAll();
                        return;
                    }
                    break;
                case 3:
                    if (Convert.ToInt16(textBox6.Text) < 1 || Convert.ToInt16(textBox6.Text) > 8)
                    {
                        MessageBox.Show("Please input Length of Tag Data between 1 and 6 Word !");
                        textBox6.Focus();
                        textBox6.SelectAll();
                        return;
                    }
                    break;
            }

            if (textBox8.Text.Length == 0 || textBox8.Text.Length / 4 < Convert.ToInt16(textBox6.Text))
            {
                MessageBox.Show("Please input enough Length of Tag Data!");
                return;
            }
            /////////////////////////////////

            str = textBox8.Text;
            for (i = 0; i < textBox8.Text.Length / 2; i++)
            {
                mask[i] = (byte)Convert.ToInt16((str[i * 2].ToString() + str[i * 2 + 1].ToString()), 16);
            }

            m_antenna_sel = 1;
            
            len = Convert.ToInt16(textBox6.Text);
            if (mem == 1)
            {
                for (i = 0; i < 5; i++)
                {
                    Thread.Sleep(30);

                    res = ReaderOTR2000API.EPC1G2_WriteEPC(m_hScanner, Convert.ToByte(textBox6.Text), mask, AccessPassWord);
                    if ((res == OK) || (res == 5) || (res == 8) || (res == 9))
                        break;
                    
                }
            }
            else
            {
                if (comboBox3.SelectedIndex < 0)
                {
                    MessageBox.Show("Please read first than choose a tag!");
                    return;
                }
                switch (mem)
                {
                    case 0:
                    case 2:
                        if (Convert.ToInt16(textBox5.Text) < 0 || Convert.ToInt16(textBox5.Text) > 3)
                        {
                            MessageBox.Show("Please input Location of Tag Address between 0 and 4!");
                            textBox5.Focus();
                            textBox5.SelectAll();
                            return;
                        }
                        if ((len + Convert.ToInt16(textBox5.Text)) > 4)
                        {
                            len = 4 - Convert.ToInt16(textBox5.Text);
                        }
                        break;
                    case 3:
                        if (Convert.ToInt16(textBox5.Text) < 0)
                        {
                            MessageBox.Show("Please input Location of Tag Address more than 0!");
                            textBox5.Focus();
                            textBox5.SelectAll();
                            return;
                        }
                        break;
                }
                EPC_Word = TagBuffer[comboBox3.SelectedIndex, 0];
                for (i = 0; i < TagBuffer[comboBox3.SelectedIndex, 0] * 2; i++)
                {
                    IDTemp[i] = TagBuffer[comboBox3.SelectedIndex, i + 1];
                }

                for (i = 0; i < 5; i++)
                {
                    Thread.Sleep(30);

                    res = ReaderOTR2000API.EPC1G2_WriteWordBlock(m_hScanner, Convert.ToByte(EPC_Word), IDTemp, Convert.ToByte(mem), Convert.ToByte(textBox5.Text), Convert.ToByte(len), mask, AccessPassWord);
                    
                    if ((res == OK) || (res == 5) || (res == 8) || (res == 9))
                        break;
                }
            }

            if (res == OK)
            {
                listBox1.Items.Add("Write Successfully!");
            }
            else
            {
                this.ReportError(res, ref str);
                if (str == "Unbeknown Error!")
                    str = "Write Fail!";
                if (res == 9)
                    str = "The access password is error!";
                listBox1.Items.Add(str);
            }
        }
        #endregion

        #region ErrorInformation
        public void ReportError(int res, ref string temp)
        {
            switch (res)
            {
                case 1:
                    temp = "Connect antenna fail!";
                    break;
                case 2:
                    temp = "No Tag!";
                    break;
                case 3:
                    temp = "Illegal Tag!";
                    break;
                case 4:
                    temp = "Power is not enough!";
                    break;
                case 5:
                    temp = "The memory has been protected!";
                    break;
                case 6:
                    temp = "Check sum error!";
                    break;
                case 7:
                    temp = "Parameter error!";
                    break;
                case 8:
                    temp = "The memory don't exist!";
                    break;
                case 9:
                    temp = "The Access Password is error!";
                    break;
                case 10:
                    temp = "The Kill Password cannot be 000000!";
                    break;
                case 14:
                    temp = "Locked Tags in the field!";
                    break;
                case 30:
                    temp = "Invalid Command!";
                    break;
                case 31:
                    temp = "Other Error!";
                    break;
                default:
                    temp = "Unbeknown Error!";
                    break;
            }
        }
        #endregion


        #region List 6B Tag ID
        private void button1_Click(object sender, EventArgs e)
        {
            if (m_hScanner == -1)
                return;
            nidEvent = 3;
            string str, strtemp;
            int i = 0;
            int j = 0;
            

            if (button1.Text == "List Tag ID")
            {

                button1.Text = "Stop";
                Read_times = 0;
                k = 0;
                count_test = 0;
                listView1.Items.Clear();
                timer1.Interval = 50;
                timer1.Enabled = true;
            }
            else
            {
                button1.Text = "List Tag ID";
                timer1.Enabled = false;
                comboBox1.Items.Clear();
                listView1.Items.Clear();

                byte[] dada = new byte[1024];
                for (i = 0; i < k; i++)
                {
                    str = listView2.Items[i].SubItems[2].Text;
                    TagBuffer[i, 0] = Convert.ToByte(str);
                    str = listView2.Items[i].SubItems[1].Text;
                    for (j = 0; j < str.Length / 2; j++)
                    {
                        strtemp = str[j * 2].ToString() + str[j * 2 + 1].ToString();
                        TagBuffer[i, j + 1] = (byte)Convert.ToInt16(strtemp, 16);
                    }
                    strtemp = "";
                    strtemp = (i + 1).ToString("D2") + ".";
                    comboBox1.Items.Add(strtemp + str);

                }
                if (k != 0)
                {
                    comboBox1.SelectedIndex = 0;

                }
            }

        }
        #endregion

        #region 6B_Read
        private void button4_Click(object sender, EventArgs e)
        {
            if (m_hScanner == -1)
                return;
            int i;
            nidEvent = 4;


            if (Convert.ToInt16(textBox10.Text) < 0 || Convert.ToInt16(textBox10.Text) > 223)
            {
                MessageBox.Show("Please input start address of Tag between 0 and 223!");
                textBox10.Focus();
                textBox10.SelectAll();
                return;
            }

            if (Convert.ToInt16(textBox9.Text) < 1 || Convert.ToInt16(textBox9.Text) > 32)
            {
                MessageBox.Show("Please input length of data between 1 and 32!");
                textBox9.Focus();
                textBox9.SelectAll();
                return;
            }


            Interval = 50;

            ptr = Convert.ToInt16(textBox10.Text);
            len = Convert.ToInt16(textBox9.Text);
            if (ptr + len > 224)
                len = 224 - ptr;

            for (i = 0; i < 8; i++)
            {
                IDTemp[i] = TagNumber[comboBox1.SelectedIndex, i];
            }

            if (button4.Text == "Read")
            {
                m_antenna_sel = 1;


                button4.Text = "Stop";
                Read_times = 0;
                //                k = 0;
                //                listView1.Items.Clear();
                listBox2.Items.Clear();
                timer1.Interval = Interval;
                timer1.Enabled = true;
            }
            else
            {
                button4.Text = "Read";
                timer1.Enabled = false;
            }

        }
        #endregion

        #region Write Byte
        private void button2_Click(object sender, EventArgs e)
        {
            if (m_hScanner == -1)
                return;
            int i;
            string str;
            nidEvent = 5;

            if (Convert.ToInt16(textBox10.Text) < 8 || Convert.ToInt16(textBox10.Text) > 223)
            {
                MessageBox.Show("Please input start address of Tag between 8 and 223!", "Warning");
                textBox10.Focus();
                textBox10.SelectAll();
                return;
            }

            if (Convert.ToInt16(textBox9.Text) < 1 || Convert.ToInt16(textBox9.Text) > 16)
            {
                MessageBox.Show("Please input length of data between 1 and 16!", "Warning");
                textBox9.Focus();
                textBox9.SelectAll();
                return;
            }


            Interval = 500;

            ptr = Convert.ToInt16(textBox10.Text);
            len = Convert.ToInt16(textBox9.Text);
            if (ptr + len > 224)
                len = 224 - ptr;

            for (i = 0; i < 8; i++)
            {
                IDTemp[i] = TagNumber[comboBox1.SelectedIndex, i];
            }

            str = textBox4.Text;
            for (i = 0; i < textBox4.Text.Length / 2; i++)
            {
                mask[i] = (byte)Convert.ToInt16((str[i * 2].ToString() + str[i * 2 + 1].ToString()), 16);
            }

            if (button2.Text == "Write")
            {
                m_antenna_sel = 1;

                button2.Text = "Stop";
                Read_times = 0;
                //                k = 0;
                //                listView1.Items.Clear();
                listBox2.Items.Clear();
                timer1.Interval = Interval;
                timer1.Enabled = true;
            }
            else
            {
                button2.Text = "Write";
                timer1.Enabled = false;
            }
        }
        #endregion

        


    }
}
